﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using MODEL;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
namespace winUi
{
    public partial class FormQuery : Form
    {
        DataTable LoadData = new DataTable();
        public int index = 0;
        public FormQuery()
        {
            InitializeComponent();
        }

        private void FormQuery_Load(object sender, EventArgs e)
        {
            string[] types = new string[]
            {
                "入库",
                "出库",
                "退库"
            };
            cbType.DataSource = types;
            cbType.SelectedIndex = index;
            dgvData.MultiSelect = false;
            dgvData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvData.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadDataByCon();

        }
        public void LoadDataByCon()
        {
            string type = cbType.SelectedValue.ToString();
            string py = tbKey.Text;
            if (type == "入库")
            {
                //dgvData.DataSource = null;
                //dgvData.DataSource = new BllRuKu().GetList(py, dtpSDate.Value, dtpEdate.Value);
                LoadData = new BllRuKu().GetList(py, dtpSDate.Value, dtpEdate.Value);
            }
            if (type == "出库")
            {
                //dgvData.DataSource = null;
                //dgvData.DataSource = new BllChuKu().GetList(py, dtpSDate.Value, dtpEdate.Value);
                LoadData = new BllChuKu().GetList(py, dtpSDate.Value, dtpEdate.Value);
            }
            if (type == "退库")
            {
                //dgvData.DataSource = new BllTuiKu().GetListByPy(py);
                //dgvData.DataSource = new BllTuiKu().GetListBy(py);
                //dgvData.DataSource = null;
                //dgvData.DataSource = new BllTuiKu().GetList(py,dtpSDate.Value,dtpEdate.Value);
                LoadData = new BllTuiKu().GetList(py, dtpSDate.Value, dtpEdate.Value);
            }
            dgvData.DataSource = null;
            dgvData.DataSource = LoadData;
        }
        private void btnExport_Click(object sender, EventArgs e)
        {
            //SaveFileDialog sfd = new SaveFileDialog();
            string type = cbType.SelectedValue.ToString();
            //string py = tbKey.Text;
            FolderBrowserDialog sfd = new FolderBrowserDialog();
            sfd.RootFolder = Environment.SpecialFolder.CommonDesktopDirectory;
            DialogResult result = sfd.ShowDialog();
            string fileName = type + dtpSDate.Value.ToShortDateString() + "-" + dtpEdate.Value.ToShortDateString() + ".xls";
            if (result == DialogResult.OK)
            {
                if (type == "入库")
                {
                    fileName = Path.Combine(sfd.SelectedPath, fileName);
                    //fileName = sfd.SelectedPath+ + fileName;
                    FileStream fs = new FileStream(fileName, FileMode.Create);
                    HSSFWorkbook wb = new HSSFWorkbook();
                    ISheet sheet = wb.CreateSheet("入库表");
                    //创建表头
                    IRow headerRow = sheet.CreateRow(0);
                    headerRow.CreateCell(0).SetCellValue("日期");
                    headerRow.CreateCell(1).SetCellValue("品名");
                    headerRow.CreateCell(2).SetCellValue("型号");
                    headerRow.CreateCell(3).SetCellValue("规格");
                    headerRow.CreateCell(4).SetCellValue("单位");
                    headerRow.CreateCell(5).SetCellValue("入库数量");
                    headerRow.CreateCell(6).SetCellValue("备注");
                    for (int i = 1; i <= LoadData.Rows.Count; i++)
                    {
                        IRow row = sheet.CreateRow(i);
                        DataRow datarow = LoadData.Rows[i - 1];
                        row.CreateCell(0).SetCellValue(((DateTime)datarow[9]).ToShortDateString());//日期
                        row.CreateCell(1).SetCellValue(datarow[1].ToString());//品名
                        row.CreateCell(2).SetCellValue(datarow[2].ToString());//型号
                        row.CreateCell(3).SetCellValue(datarow[3].ToString());//规格
                        row.CreateCell(4).SetCellValue(datarow[4].ToString());//单位
                        row.CreateCell(5).SetCellValue((int)datarow[7]);//入库数量
                        row.CreateCell(6).SetCellValue(datarow[10].ToString());//备注

                    }
                    wb.Write(fs);
                    fs.Close();
                }
                if (type == "出库")
                {
                    fileName = Path.Combine(sfd.SelectedPath, fileName);
                    //fileName = sfd.SelectedPath+ + fileName;
                    FileStream fs = new FileStream(fileName, FileMode.Create);
                    HSSFWorkbook wb = new HSSFWorkbook();
                    ISheet sheet = wb.CreateSheet("出库表");
                    //创建表头
                    IRow headerRow = sheet.CreateRow(0);
                    headerRow.CreateCell(0).SetCellValue("日期");
                    headerRow.CreateCell(1).SetCellValue("品名");
                    headerRow.CreateCell(2).SetCellValue("型号");
                    headerRow.CreateCell(3).SetCellValue("规格");
                    headerRow.CreateCell(4).SetCellValue("单位");
                    headerRow.CreateCell(5).SetCellValue("出库数量");
                    headerRow.CreateCell(6).SetCellValue("备注");
                    for (int i = 1; i <= LoadData.Rows.Count; i++)
                    {
                        IRow row = sheet.CreateRow(i);
                        DataRow datarow = LoadData.Rows[i - 1];
                        row.CreateCell(0).SetCellValue(((DateTime)datarow[9]).ToShortDateString());//日期
                        row.CreateCell(1).SetCellValue(datarow[1].ToString());//品名
                        row.CreateCell(2).SetCellValue(datarow[2].ToString());//型号
                        row.CreateCell(3).SetCellValue(datarow[3].ToString());//规格
                        row.CreateCell(4).SetCellValue(datarow[4].ToString());//单位
                        row.CreateCell(5).SetCellValue((int)datarow[7]);//入库数量
                        row.CreateCell(6).SetCellValue(datarow[10].ToString());//备注

                    }
                    wb.Write(fs);
                    fs.Close();
                }
                if (type== "退库")
                {
                    fileName = Path.Combine(sfd.SelectedPath, fileName);
                    //fileName = sfd.SelectedPath+ + fileName;
                    FileStream fs = new FileStream(fileName, FileMode.Create);
                    HSSFWorkbook wb = new HSSFWorkbook();
                    ISheet sheet = wb.CreateSheet("退库表");
                    //创建表头
                    IRow headerRow = sheet.CreateRow(0);
                    headerRow.CreateCell(0).SetCellValue("日期");
                    headerRow.CreateCell(1).SetCellValue("品名");
                    headerRow.CreateCell(2).SetCellValue("型号");
                    headerRow.CreateCell(3).SetCellValue("规格");
                    headerRow.CreateCell(4).SetCellValue("单位");
                    headerRow.CreateCell(5).SetCellValue("退库数量");
                    headerRow.CreateCell(6).SetCellValue("备注");
                    for (int i = 1; i <= LoadData.Rows.Count; i++)
                    {
                        IRow row = sheet.CreateRow(i);
                        DataRow datarow = LoadData.Rows[i - 1];
                        row.CreateCell(0).SetCellValue(((DateTime)datarow[7]).ToShortDateString());//日期
                        row.CreateCell(1).SetCellValue(datarow[1].ToString());//品名
                        row.CreateCell(2).SetCellValue(datarow[2].ToString());//型号
                        row.CreateCell(3).SetCellValue(datarow[3].ToString());//规格
                        row.CreateCell(4).SetCellValue(datarow[4].ToString());//单位
                        row.CreateCell(5).SetCellValue((int)datarow[6]);//入库数量
                        row.CreateCell(6).SetCellValue(datarow[9].ToString());//备注

                    }
                    wb.Write(fs);
                    fs.Close();
                }
                
            }
        }

        private void tbKey_TextChanged(object sender, EventArgs e)
        {
            LoadDataByCon();
        }
    }
}
